package com.bytedance.improvingroad.activity

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.os.SystemClock
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bytedance.improvingroad.R
import com.bytedance.improvingroad.databinding.ActivityBannerBinding
import com.bytedance.improvingroad.utils.Utils
import java.lang.Thread.interrupted


class BannerActivity : BaseBindingActivity<ActivityBannerBinding>() {
    val imgView = mutableListOf<ImageView>()
    val tvTextDes = arrayOf(
        "巩俐不低俗，我就不能低俗",
        "朴树又回来了，再唱经典老歌引万人大合唱",
        "揭秘北京电影如何升级",
        "乐视网TV版大派送", "热血屌丝的反杀"
    )
    val handler = object : Handler(){
        override fun handleMessage(msg: Message) {
            //切换到主线程做事情
        }
    }
    val scrollTimeOffSet = 2 * 1000
    var preDotPos = 0
    override fun initData() {
        Thread{
            Looper.prepare()
            Looper.loop()
            handler.sendEmptyMessage(0)
        }.start()
        val imgs = arrayOf(
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5
        )
        imgs.forEach { imgRes ->
            ImageView(this).apply {
                setBackgroundResource(imgRes)
                imgView.add(this)
            }
            View(this).apply {
                setBackgroundResource(R.drawable.dot_bg_selector)
                layoutParams =  ViewGroup.MarginLayoutParams(Utils.dp2px(10), Utils.dp2px(10)).apply { leftMargin = Utils.dp2px(5) }
                isEnabled = false
                binding.dotGroup.addView(this)
            }
        }
        binding.viewPager.adapter = BannerAdapter()
        binding.tvBannerDes.text = tvTextDes[0]
        binding.dotGroup.getChildAt(0).isEnabled = true
        binding.viewPager.currentItem = 0
    }

    override fun initListener() {
        binding.viewPager.setOnPageChangeListener(BannerPageChangeListener())
        Thread{
            while (!interrupted()) {
                SystemClock.sleep(scrollTimeOffSet.toLong())
                runOnUiThread {
                    Log.e("cx","currentItem更新")
                    val newIndex = binding.viewPager.currentItem + 1
                    binding.viewPager.currentItem = newIndex

                }
            }
        }.start()
    }

    private inner class BannerAdapter: PagerAdapter() {
        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(imgView[position % imgView.size])
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = imgView[position % imgView.size].apply {
                setOnClickListener { view->
                    Toast.makeText(this@BannerActivity, "Page 被点击了", Toast.LENGTH_SHORT).show()
                }
            }
            container.addView(view)
            return view
        }

        override fun getCount(): Int {
            return Int.MAX_VALUE
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }
    }

    private inner class BannerPageChangeListener: ViewPager.OnPageChangeListener {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {

        }

        override fun onPageSelected(position: Int) {
            val newPos = position%imgView.size
            binding.tvBannerDes.text = tvTextDes[newPos]
            binding.dotGroup.getChildAt(preDotPos).isEnabled = false
            binding.dotGroup.getChildAt(newPos).isEnabled = true
            preDotPos = newPos
        }

        override fun onPageScrollStateChanged(state: Int) {

        }

    }

}